package com.xbongbong.paas.service.log.crm.operation.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.service.log.crm.operation.SignInOperationLogHandle;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.log.pojo.dto.AddSignInDTO;
import com.xbongbong.pro.log.pojo.dto.DeleteSignInDTO;
import com.xbongbong.pro.weblist.pojo.dto.SignInDeleteDTO;
import com.xbongbong.pro.weblist.pojo.dto.SignInSaveDTO;
import com.xbongbong.saas.domain.entity.SignInEntity;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: yingjf
 * @date: 2022/5/6 10:34
 * @description:
 */
@Slf4j
@Service("signInOperationLogHandleImpl")
public class SignInOperationLogHandleImpl implements SignInOperationLogHandle {
    @Resource
    private MongoLogHelp mongoLogHelp;

    @Override
    @Async("sysLogThreadPool")
    public void add(AddSignInDTO addSignInDTO) {
        try {
            SignInEntity signInEntity = addSignInDTO.getSignInEntity();
            SignInSaveDTO signInSaveDTO = addSignInDTO.getSignInSaveDTO();
            String address = signInEntity.getProvince() + signInEntity.getCity() + signInEntity.getDistrict() + signInEntity.getAddress();
            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.AGG_MEMO_ADD_SIGN), signInSaveDTO.getLoginUserName(),address );
            String opObjectName = "";
            if (StringUtil.isNotEmpty(signInEntity.getCustomerName())) {
                opObjectName = signInEntity.getCustomerName();
            }
            mongoLogHelp.buildLog(signInSaveDTO.getCorpid(), signInSaveDTO.getUserId(), signInSaveDTO.getLoginUserName(), OperateModuleTypeEnum.SIGNIN, OperateTypeEnum.NEW,
                    signInEntity.getId().toString(), opObjectName, memo, signInSaveDTO.getHttpHeader());
        }catch (XbbException e){
            log.error("com.xbongbong.paas.service.log.crm.operation.impl.SignInOperationLogHandleImpl.add",e);
        }
    }

    @Override
    @Async("sysLogThreadPool")
    public void delete(DeleteSignInDTO deleteSignInDTO) {
        try {
            SignInEntity signInEntity = deleteSignInDTO.getSignInEntity();
            SignInDeleteDTO signInDeleteDTO = deleteSignInDTO.getSignInDeleteDTO();
            String address = signInEntity.getProvince() + signInEntity.getCity() + signInEntity.getDistrict() + signInEntity.getAddress();
            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.AGG_MEMO_DELETE_SIGN), signInDeleteDTO.getLoginUserName(),address );
            String opObjectName = "";
            if (StringUtil.isNotEmpty(signInEntity.getCustomerName())) {
                opObjectName = signInEntity.getCustomerName();
            }
            mongoLogHelp.buildLog(signInDeleteDTO.getCorpid(), signInDeleteDTO.getUserId(), signInDeleteDTO.getLoginUserName(), OperateModuleTypeEnum.SIGNIN, OperateTypeEnum.DELETE,
                    signInDeleteDTO.getSignInId().toString(), opObjectName, memo, signInDeleteDTO.getHttpHeader());
        }catch (XbbException e){
            log.error("com.xbongbong.paas.service.log.crm.operation.impl.SignInOperationLogHandleImpl.delete",e);
        }
    }
}
